<?php 

namespace app\admin\controller;
use app\admin\controller\Base;
use app\admin\model\blog;
use app\admin\model\category;
use app\admin\model\comment;
use think\Request;
use think\Db;
class Artice extends Base{
	//判断是否是POST请求
	public function ispost(){
		if (!Request::instance()->isPost()){
			$this->redirect('index');
		}
	}
	public function index(){
		//关联查询，查询文章所属分类
		$list = Db::table('xlblog_category')
		->where('type', '=', 'artice')
		->alias('a')
		->join('xlblog_blog w','a.id = w.cid')
		->select();
		return $this->fetch('admin/artice',['artice'=>$list]);
	}
	public function articeedit(){
		//获取id
		$id = input('id');
		$category = new category;
		$artice   = new blog;
		//获取当前已有的分类
		$category=$category->order('id','desc')->select();
		//获取要编辑的文章内容
		$artice = $artice->where('id', '=', $id)->find();
		return $this->fetch('admin/articeedit',['category'=>$category,'artice'=>$artice]);
	}
	public function articeadd(){
		//获取当前已有的分类
		$category = new category;
		$category=$category->order('id','desc')->select();
		return $this->fetch('admin/articeadd',['category'=>$category]);
	}
	public function articeclass(){
		$category = new category;
		$category=$category->order('id','desc')->select();
		return $this->fetch('admin/articeclass',['category'=>$category]);
	}
	//添加分类
	public function atadd(){
		$this->ispost();
		$category = new category;
		$category->name = input('name');
		if($category->save()){
			$data = ["result"=>1, "msg"=>"添加成功", "id"=>$category->id];
			echo json_encode($data);
		}else{
			$data = ["result"=>2, "msg"=>"添加失败"];
			echo json_encode($data);
		}
	}
	//添加文章
	public function acadd(){
		$this->ispost();
		$cid = input('cid');
		if (!is_numeric($cid)) {
			$cid = 1;
		}
		$artice = new blog;
		$type = input('type');
		$artice->title 		= input('title');
		$artice->content 	= input('content');
		$artice->abstract 	= input('abstract');
		$artice->author 	= input('author');
		$artice->time 		= time();
		$artice->cid 		= $cid;
		$artice->keyword 	= input('keyword');
		$artice->show 		= input('show');
		$artice->top 		= input('top');
		$artice->type 		= $type;
		//分类文章数量增加
		$category = category::get($cid);
		$category->count = $category->count+1;
		$category->save();
		if ($artice->save()) {
			if ($type == 'artice') {
				$url = URL('Artice/index');
			}else{
				$url = URL('Page/index');
			}
			$data = ["result"=>1, "msg"=>"发布成功","url"=>$url];
			echo json_encode($data);
		}else{
			$data = ["result"=>2, "msg"=>"发布失败"];
			echo json_encode($data);
		}
	}
	//修改文章
	public function acedit(){
		$this->ispost();
		$id = input('id');
		$artice = blog::get($id);
		$type = input('type');
		$artice->title 		= input('title');
		$artice->content 	= input('content');
		$artice->abstract 	= input('abstract');
		$artice->author 	= input('author');
		$artice->cid 		= input('cid');
		$artice->keyword 	= input('keyword');
		$artice->show 		= input('show');
		$artice->top 		= input('top');
		$artice->type 		= $type;
		if ($artice->save()) {
			if ($type == 'artice') {
				$url = URL('Artice/index');
			}else{
				$url = URL('Page/index');
			}
			$data = ["result"=>1, "msg"=>"编辑成功","url"=>$url];
			echo json_encode($data);
		}else{
			$data = ["result"=>2, "msg"=>"编辑失败"];
			echo json_encode($data);
		}
	}
	//删除文章-并删除此文章所有评论
	public function acdel(){
		$this->ispost();
		$id = input('id');
		$artice = blog::get($id);
		//分类文章数量减少
		$category = category::get($artice->cid);
		$category->count = $category->count-1;
		$category->save();
		//删除此文章所有评论
		$comment = new comment;
		$comment->where('ac_id', '=', $id)->delete();
		if($artice->delete()){
			$data = ["result"=>1, "msg"=>"删除成功"];
			echo json_encode($data);
		}else{
			$data = ["result"=>2, "msg"=>"删除失败"];
			echo json_encode($data);
		}
	}
	//修改显示状态
	public function showstatus(){
		$this->ispost();
		$id = input('id');
		$blog = blog::get($id)->toArray();
		//判断当前状态
		if ($blog['show'] == 1) {
			$blog = blog::get($id);
			$blog->show = 0;
			$blog->save();
			$data = ["result"=>1, "msg"=>"修改成功"];
			echo json_encode($data);
		}else{
			$blog = blog::get($id);
			$blog->show = 1;
			$blog->save();
			$data = ["result"=>1, "msg"=>"修改成功"];
			echo json_encode($data);
		}
	}
	//修改推荐状态
	public function topstatus(){
		$this->ispost();
		$id = input('id');
		$blog = blog::get($id)->toArray();
		//判断当前状态
		if ($blog['top'] == 1) {
			$blog = blog::get($id);
			$blog->top = 0;
			$blog->save();
			$data = ["result"=>1, "msg"=>"修改成功"];
			echo json_encode($data);
		}else{
			$blog = blog::get($id);
			$blog->top = 1;
			$blog->save();
			$data = ["result"=>1, "msg"=>"修改成功"];
			echo json_encode($data);
		}
	}
	//文章图片上传接口
	public function imgupload(){
		$file = request()->file('images');
    // 移动到框架应用根目录/public/uploads/images 目录下
		$info = $file->validate(['ext'=>'jpg,png,gif'])->move(ROOT_PATH . 'public' . DS . 'uploads/images/');
		if($info){
			$data = [
				'errno'=>0,
				'data'=>[
					'../../../' . DS . 'uploads/images/' . $info->getSaveName()
				]
			];
			echo json_encode($data);
		}else{
			$data = [
				'code'=>1,
			];
			echo json_encode($data);
		}
	}
	//删除分类-并修改文章分类为默认分类
	public function catdel(){
		$this->ispost();
		$id = input('id');
		if ($id == 1) {
			$data = ["result"=>2, "msg"=>"默认分类不允许删除!"];
			echo json_encode($data);
			exit;
		}
		$category = category::get($id);
		$blog = new blog;
		$num = $blog->save(['cid' => '1'],function($query){
		    $query->where('cid', '=', input('id'));
		});
		if($category->delete()){
			//删除成功后默认分类添加文章数量
			$category = category::get(1);
			$category->count = $category->count+$num;
			$category->save();
			$data = ["result"=>1, "msg"=>"删除成功"];
			echo json_encode($data);
		}else{
			$data = ["result"=>2, "msg"=>"删除失败"];
			echo json_encode($data);
		}
	}
	//修改分类名称
	public function catedit(){
		$this->ispost();
		$id = input('id');
		if ($id == 1) {
			$data = ["result"=>2, "msg"=>"默认分类不允许编辑!"];
			echo json_encode($data);
			exit;
		}
		$name = input('name');
		$category = category::get($id);
		$category->name = $name;
		if ($category->save()) {
			$data = ["result"=>1, "msg"=>"编辑成功"];
			echo json_encode($data);
		}else{
			$data = ["result"=>2, "msg"=>"编辑失败"];
			echo json_encode($data);
		}
	}

}


 ?>